package binaryTree.basic;

public class BinaryTreeChildCount {
    // 递归求二叉树每个节点的子节点数目
    public static int getChildCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftChildCount = getChildCount(root.left);
        int rightChildCount = getChildCount(root.right);
        return leftChildCount + rightChildCount + 1;
    }
    public static void main(String[] args) {
        // 创建二叉树
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);
        // 求二叉树每个节点的子节点数目
        System.out.println(getChildCount(root));
    }
}
